package cc.mrbird.assets.controller;

import cc.mrbird.assets.domain.AssetType;
import cc.mrbird.assets.service.AssetTypeService;
import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.unit.domain.UnitType;
import cc.mrbird.unit.service.UnitTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(description = "资产类型管理接口")
@Controller
public class AssetTypeController extends BaseController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AssetTypeService assetTypeService;
    @Log("获取资产类型信息")
    @RequestMapping(value = "assettype",method = RequestMethod.POST)
    /* @RequiresPermissions("assettype:list")*/
    public String index() {
        return "assets/assettype/assettype";
    }
    @Log("获取资产类型信息")
    @ApiOperation(value = "获取资产类型信息" ,  notes="获取资产类型信息")
    @RequestMapping(value = "assettype/list",method = RequestMethod.POST)
    /*@RequiresPermissions("assettype:list")*/
    @ResponseBody
    public Map<String, Object> assettypeList(QueryRequest request, AssetType assetType) {
        return super.selectByPageNumSize(request, () -> this.assetTypeService.findAllList(assetType, request));
    }
    @ApiOperation(value = "资产类型添加")
    @RequestMapping(value = "assettype/add",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo add(AssetType assetType) {
        try {
            AssetType result = this.assetTypeService.findByName(assetType.getAsset_type_name());
            if (result != null) {
                return ResponseBo.warn("该资产类型已存在！");
            }
            this.assetTypeService.add(assetType);
            return ResponseBo.ok();
        } catch (Exception e) {
            log.error("添加失败", e);
            return ResponseBo.error("添加失败，请重新添加！");
        }
    }
    @ApiOperation(value = "修改资产类型信息")
    @Log("修改资产类型信息")
    /* @RequiresPermissions("assettype:update")*/
    @RequestMapping(value = "assettype/update" ,method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo update(AssetType assetType) {
        try {
            this.assetTypeService.update(assetType);
            return ResponseBo.ok("修改资产类型信息成功！");
        } catch (Exception e) {
            log.error("修改资产类型信息失败", e);
            return ResponseBo.error("修改资产类型信息失败，请联系网站管理员！");
        }
    }
    @Log("删除资产类型信息")
    /*@RequiresPermissions("assettype:delete")*/
    @RequestMapping(value = "assettype/delete",method = RequestMethod.GET)
    @ResponseBody
    public ResponseBo delete(String ids) {
        try {
            this.assetTypeService.delete(ids);
            return ResponseBo.ok("删除资产类型信息成功！");
        } catch (Exception e) {
            log.error("删除资产类型信息失败", e);
            return ResponseBo.error("删除资产类型 信息失败，请联系网站管理员！");
        }
    }
}
